﻿using System;
using System.Collections.Generic;

namespace Sessionizer.Core.Unity
{
    public sealed class SessionConfigurationBuilder : ISessionConfigurationBuilder
    {
        private Dictionary<Type, Type> _declarations;

        public SessionConfigurationBuilder()
        {
            _declarations = new Dictionary<Type, Type>();
        }

        public ISessionConfigurationBuilder Declare<TData, TFactory>() where TFactory : ISessionsFactory<TData>
        {
            Type dataType = typeof (TData);
            if (_declarations.ContainsKey(dataType))
            {
                throw new InvalidOperationException("Type is already declared");
            }
            _declarations.Add(dataType, typeof (TFactory));
            return this;
        }

        public ISessionConfiguration Build()
        {
            var sessionConfiguration = new SessionConfiguration(_declarations);
            _declarations = new Dictionary<Type, Type>();
            return sessionConfiguration;
        }
    }
}